home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / CompiledEffect / CompiledEffect.cpp next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  23.9 KB  |  510 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: CompiledEffect.cpp
  3. //
  4. // Desc: This sample shows how an ID3DXEffect object can be compiled when the 
  5. //       project is built and loaded directly as a binary file at run time.
  6. //
  7. // Copyright (c) Microsoft Corporation. All rights reserved.
  8. //--------------------------------------------------------------------------------------
  9. #include "dxstdafx.h"
  10. #include "resource.h"
  11.  
  12. //#define DEBUG_VS   // Uncomment this line to debug vertex shaders 
  13. //#define DEBUG_PS   // Uncomment this line to debug pixel shaders 
  14.  
  15.  
  16. //--------------------------------------------------------------------------------------
  17. // Global variables
  18. //--------------------------------------------------------------------------------------
  19. ID3DXFont*              g_pFont = NULL;         // Font for drawing text
  20. ID3DXSprite*            g_pTextSprite = NULL;   // Sprite for batching draw text calls
  21. ID3DXEffect*            g_pEffect = NULL;       // D3DX effect interface
  22. CModelViewerCamera      g_Camera;               // A model viewing camera
  23. bool                    g_bShowHelp = true;     // If true, it renders the UI control text
  24. CDXUTDialog             g_HUD;                  // dialog for standard controls
  25. CDXUTMesh               g_Mesh;                 // Mesh object
  26. D3DXVECTOR3             g_vObjectCenter;        // Center of bounding sphere of object
  27. FLOAT                   g_fObjectRadius;        // Radius of bounding sphere of object
  28. D3DXMATRIXA16           g_mCenterWorld;         // World matrix to center the mesh
  29.  
  30. D3DXHANDLE              g_hTime = NULL;                        // Handle to var in the effect 
  31. D3DXHANDLE              g_hWorld = NULL;                       // Handle to var in the effect 
  32. D3DXHANDLE              g_hMeshTexture = NULL;                 // Handle to var in the effect 
  33. D3DXHANDLE              g_hWorldViewProjection = NULL;         // Handle to var in the effect 
  34. D3DXHANDLE              g_hTechniqueRenderScene = NULL;        // Handle to technique in the effect 
  35.  
  36.  
  37. //--------------------------------------------------------------------------------------
  38. // UI control IDs
  39. //--------------------------------------------------------------------------------------
  40. #define IDC_TOGGLEFULLSCREEN    1
  41. #define IDC_TOGGLEREF           3
  42. #define IDC_CHANGEDEVICE        4
  43.  
  44.  
  45.  
  46. //--------------------------------------------------------------------------------------
  47. // Forward declarations 
  48. //--------------------------------------------------------------------------------------
  49. bool    CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed );
  50. void    CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps );
  51. HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc );
  52. HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc );
  53. void    CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime );
  54. void    CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime );
  55. LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing );
  56. void    CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown  );
  57. void    CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl );
  58. void    CALLBACK OnLostDevice();
  59. void    CALLBACK OnDestroyDevice();
  60.  
  61. void    InitApp();
  62. void    RenderText();
  63.  
  64.  
  65. //--------------------------------------------------------------------------------------
  66. // Entry point to the program. Initializes everything and goes into a message processing 
  67. // loop. Idle time is used to render the scene.
  68. //--------------------------------------------------------------------------------------
  69. INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
  70. {
  71.     // Set the callback functions. These functions allow the sample framework to notify
  72.     // the application about device changes, user input, and windows messages.  The 
  73.     // callbacks are optional so you need only set callbacks for events you're interested 
  74.     // in. However, if you don't handle the device reset/lost callbacks then the sample 
  75.     // framework won't be able to reset your device since the application must first 
  76.     // release all device resources before resetting.  Likewise, if you don't handle the 
  77.     // device created/destroyed callbacks then the sample framework won't be able to 
  78.     // recreate your device resources.
  79.     DXUTSetCallbackDeviceCreated( OnCreateDevice );
  80.     DXUTSetCallbackDeviceReset( OnResetDevice );
  81.     DXUTSetCallbackDeviceLost( OnLostDevice );
  82.     DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
  83.     DXUTSetCallbackMsgProc( MsgProc );
  84.     DXUTSetCallbackKeyboard( KeyboardProc );
  85.     DXUTSetCallbackFrameRender( OnFrameRender );
  86.     DXUTSetCallbackFrameMove( OnFrameMove );
  87.  
  88.     // Show the cursor and clip it when in full screen
  89.     DXUTSetCursorSettings( true, true );
  90.  
  91.     InitApp();
  92.  
  93.     // Initialize the sample framework and create the desired Win32 window and Direct3D 
  94.     // device for the application. Calling each of these functions is optional, but they
  95.     // allow you to set several options which control the behavior of the framework.
  96.     DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
  97.     DXUTCreateWindow( L"CompiledEffect" );
  98.     DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, IsDeviceAcceptable, ModifyDeviceSettings );
  99.  
  100.     // Pass control to the sample framework for handling the message pump and 
  101.     // dispatching render calls. The sample framework will call your FrameMove 
  102.     // and FrameRender callback when there is idle time between handling window messages.
  103.     DXUTMainLoop();
  104.  
  105.     // Perform any application-level cleanup here. Direct3D device resources are released within the
  106.     // appropriate callback functions and therefore don't require any cleanup code here.
  107.  
  108.     return DXUTGetExitCode();
  109. }
  110.  
  111.  
  112. //--------------------------------------------------------------------------------------
  113. // Initialize the app 
  114. //--------------------------------------------------------------------------------------
  115. void InitApp()
  116. {
  117.     // Initialize dialogs
  118.     g_HUD.SetCallback( OnGUIEvent ); int iY = 10; 
  119.     g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 35, iY, 125, 22 );
  120.     g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 35, iY += 24, 125, 22 );
  121.     g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 35, iY += 24, 125, 22 );
  122. }
  123.  
  124.  
  125. //--------------------------------------------------------------------------------------
  126. // Called during device initialization, this code checks the device for some 
  127. // minimum set of capabilities, and rejects those that don't pass by returning false.
  128. //--------------------------------------------------------------------------------------
  129. bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
  130.                                   D3DFORMAT BackBufferFormat, bool bWindowed )
  131. {
  132.     // No fallback, so need ps1.1
  133.     if( pCaps->PixelShaderVersion < D3DPS_VERSION(1,1) )
  134.         return false;
  135.  
  136.     // Skip backbuffer formats that don't support alpha blending
  137.     IDirect3D9* pD3D = DXUTGetD3DObject(); 
  138.     if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
  139.                     AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, 
  140.                     D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
  141.         return false;
  142.  
  143.     return true;
  144. }
  145.  
  146.  
  147. //--------------------------------------------------------------------------------------
  148. // This callback function is called immediately before a device is created to allow the 
  149. // application to modify the device settings. The supplied pDeviceSettings parameter 
  150. // contains the settings that the framework has selected for the new device, and the 
  151. // application can make any desired changes directly to this structure.  Note however that 
  152. // the sample framework will not correct invalid device settings so care must be taken 
  153. // to return valid device settings, otherwise IDirect3D9::CreateDevice() will fail.  
  154. //--------------------------------------------------------------------------------------
  155. void CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps )
  156. {
  157.     // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW 
  158.     // then switch to SWVP.
  159.     if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
  160.          pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) )
  161.     {
  162.         pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  163.     }
  164.     else
  165.     {
  166.         pDeviceSettings->BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  167.     }
  168.  
  169.     // This application is designed to work on a pure device by not using 
  170.     // IDirect3D9::Get*() methods, so create a pure device if supported and using HWVP.
  171.     if ((pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE) != 0 && 
  172.         (pDeviceSettings->BehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING) != 0 )
  173.         pDeviceSettings->BehaviorFlags |= D3DCREATE_PUREDEVICE;
  174.  
  175.     // Debugging vertex shaders requires either REF or software vertex processing 
  176.     // and debugging pixel shaders requires REF.  
  177. #ifdef DEBUG_VS
  178.     if( pDeviceSettings->DeviceType != D3DDEVTYPE_REF )
  179.     {
  180.         pDeviceSettings->BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
  181.         pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE;                            
  182.         pDeviceSettings->BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  183.     }
  184. #endif
  185. #ifdef DEBUG_PS
  186.     pDeviceSettings->DeviceType = D3DDEVTYPE_REF;
  187. #endif
  188. }
  189.  
  190.  
  191. //--------------------------------------------------------------------------------------
  192. // This callback function will be called immediately after the Direct3D device has been 
  193. // created, which will happen during application initialization and windowed/full screen 
  194. // toggles. This is the best location to create D3DPOOL_MANAGED resources since these 
  195. // resources need to be reloaded whenever the device is destroyed. Resources created  
  196. // here should be released in the OnDestroyDevice callback. 
  197. //--------------------------------------------------------------------------------------
  198. HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc )
  199. {
  200.     HRESULT hr;
  201.  
  202.     // Initialize the font
  203.     V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, 
  204.                          OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, 
  205.                          L"Arial", &g_pFont ) );
  206.  
  207.     // Load the mesh
  208.     V_RETURN( g_Mesh.Create( pd3dDevice, L"dwarf\\dwarf.x" ) );
  209.  
  210.     // Find the mesh's center, then generate a centering matrix.
  211.     IDirect3DVertexBuffer9* pVB = NULL;
  212.     V_RETURN( g_Mesh.m_pSysMemMesh->GetVertexBuffer( &pVB ) );
  213.  
  214.     void* pVertices = NULL;
  215.     hr = pVB->Lock( 0, 0, &pVertices, 0 );
  216.     if( FAILED(hr) )
  217.     {
  218.         SAFE_RELEASE( pVB );
  219.         return hr;
  220.     }
  221.  
  222.     hr = D3DXComputeBoundingSphere( (D3DXVECTOR3*)pVertices, g_Mesh.m_pSysMemMesh->GetNumVertices(),
  223.                                     D3DXGetFVFVertexSize(g_Mesh.m_pSysMemMesh->GetFVF()), &g_vObjectCenter,
  224.                                     &g_fObjectRadius );
  225.     pVB->Unlock();
  226.     SAFE_RELEASE( pVB );
  227.  
  228.     if( FAILED(hr) )
  229.         return hr;
  230.  
  231.     D3DXMatrixTranslation( &g_mCenterWorld, -g_vObjectCenter.x, -g_vObjectCenter.y, -g_vObjectCenter.z );
  232.  
  233.     // Read the D3DX effect file
  234.     TCHAR str[MAX_PATH];
  235.     hr = DXUTFindDXSDKMediaFileCch( str, MAX_PATH, TEXT("CompiledEffect.fxo") );
  236.     if( FAILED(hr) )
  237.     {
  238.         MessageBox( DXUTGetHWND(), TEXT("Could not locate \"CompiledEffect.fxo\".\n\n")
  239.                                    TEXT("This file is created as part of the project build process,\n")
  240.                                    TEXT("so the associated project must be compiled inside Visual\n")
  241.                                    TEXT("Studio before attempting to run this sample.\n\n") 
  242.                                    TEXT("If receiving this error even after compiling the project,\n")
  243.                                    TEXT("it's likely there was a problem compiling the effect file.\n")
  244.                                    TEXT("Check the build log to verify the custom build step was\n")
  245.                                    TEXT("run and to look for possible fxc compile errors."), 
  246.                                    TEXT("File Not Found"), MB_OK );
  247.         return E_FAIL;
  248.     }
  249.  
  250.     // Since we are loading a binary file here and this effect has already been compiled,
  251.     // you can not pass compiler flags here (for example to debug the shaders). 
  252.     // To debug the shaders, you must pass these flags to the compiler that generated the
  253.     // binary (for example fxc.exe).  In this sample, there are 2 extra Visual Studio configurations
  254.     // called "Debug Shaders" and "Unicode Debug Shaders" that pass the debug shader flags to fxc.exe.
  255.     V_RETURN( D3DXCreateEffectFromFile(pd3dDevice, str, NULL, NULL, 0, NULL, &g_pEffect, NULL ) );
  256.  
  257.     // Setup the camera's view parameters
  258.     D3DXVECTOR3 vecEye(0.0f, 0.0f, -5.0f);
  259.     D3DXVECTOR3 vecAt (0.0f, 0.0f, -0.0f);
  260.     g_Camera.SetViewParams( &vecEye, &vecAt );
  261.  
  262.     return S_OK;
  263. }
  264.  
  265.  
  266. //--------------------------------------------------------------------------------------
  267. // This callback function will be called immediately after the Direct3D device has been 
  268. // reset, which will happen after a lost device scenario. This is the best location to 
  269. // create D3DPOOL_DEFAULT resources since these resources need to be reloaded whenever 
  270. // the device is lost. Resources created here should be released in the OnLostDevice 
  271. // callback. 
  272. //--------------------------------------------------------------------------------------
  273. HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, 
  274.                                 const D3DSURFACE_DESC* pBackBufferSurfaceDesc )
  275. {
  276.     HRESULT hr;
  277.  
  278.     if( g_pFont )
  279.         V_RETURN( g_pFont->OnResetDevice() );
  280.     if( g_pEffect )
  281.         V_RETURN( g_pEffect->OnResetDevice() );
  282.     g_Mesh.RestoreDeviceObjects( pd3dDevice );
  283.  
  284.     // Create a sprite to help batch calls when drawing many lines of text
  285.     V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pTextSprite ) );
  286.  
  287.     // Set effect variables as needed
  288.     D3DXCOLOR colorMtrlDiffuse(1.0f, 1.0f, 1.0f, 1.0f);
  289.     D3DXCOLOR colorMtrlAmbient(0.35f, 0.35f, 0.35f, 0);
  290.     g_pEffect->SetVector("g_MaterialAmbientColor", (D3DXVECTOR4*)&colorMtrlAmbient);
  291.     g_pEffect->SetVector("g_MaterialDiffuseColor", (D3DXVECTOR4*)&colorMtrlDiffuse);
  292.  
  293.     // To read or write to D3DX effect variables we can use the string name 
  294.     // instead of using handles, however it improves perf to use handles since then 
  295.     // D3DX won't have to spend time doing string compares
  296.     g_hTechniqueRenderScene         = g_pEffect->GetTechniqueByName( "RenderScene" );
  297.     g_hTime                         = g_pEffect->GetParameterByName( NULL, "g_fTime" );
  298.     g_hWorld                        = g_pEffect->GetParameterByName( NULL, "g_mWorld" );
  299.     g_hWorldViewProjection          = g_pEffect->GetParameterByName( NULL, "g_mWorldViewProjection" );
  300.     g_hMeshTexture                  = g_pEffect->GetParameterByName( NULL, "g_MeshTexture" );
  301.  
  302.  
  303.     // Setup the camera's projection parameters
  304.     float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;
  305.     g_Camera.SetProjParams( D3DX_PI/4, fAspectRatio, 0.1f, 1000.0f );
  306.     g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
  307.  
  308.     g_HUD.SetLocation( pBackBufferSurfaceDesc->Width-170, 0 );
  309.     g_HUD.SetSize( 170, 170 );
  310.     
  311.     return S_OK;
  312. }
  313.  
  314.  
  315. //--------------------------------------------------------------------------------------
  316. // This callback function will be called once at the beginning of every frame. This is the
  317. // best location for your application to handle updates to the scene, but is not 
  318. // intended to contain actual rendering calls, which should instead be placed in the 
  319. // OnFrameRender callback.  
  320. //--------------------------------------------------------------------------------------
  321. void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime )
  322. {
  323.     D3DXMATRIXA16 mWorld;
  324.     D3DXMATRIXA16 mView;
  325.     D3DXMATRIXA16 mProj;
  326.     D3DXMATRIXA16 mWorldViewProjection;
  327.     
  328.     // Update the camera's position based on user input 
  329.     g_Camera.FrameMove( fElapsedTime );
  330.  
  331.     // Get the projection & view matrix from the camera class
  332.     mWorld = g_mCenterWorld * *g_Camera.GetWorldMatrix();
  333.     mProj = *g_Camera.GetProjMatrix();
  334.     mView = *g_Camera.GetViewMatrix();
  335.  
  336.     mWorldViewProjection = mWorld * mView * mProj;
  337.  
  338.     // Update the effect's variables 
  339.     g_pEffect->SetMatrix( g_hWorldViewProjection, &mWorldViewProjection );
  340.     g_pEffect->SetMatrix( g_hWorld, &mWorld );
  341.     g_pEffect->SetFloat( g_hTime, (float)fTime );
  342. }
  343.  
  344.  
  345. //--------------------------------------------------------------------------------------
  346. // This callback function will be called at the end of every frame to perform all the 
  347. // rendering calls for the scene, and it will also be called if the window needs to be 
  348. // repainted. After this function has returned, the sample framework will call 
  349. // IDirect3DDevice9::Present to display the contents of the next buffer in the swap chain
  350. //--------------------------------------------------------------------------------------
  351. void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime )
  352. {
  353.     HRESULT hr;
  354.     
  355.     // Clear the render target and the zbuffer 
  356.     V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0) );
  357.  
  358.     // Render the scene
  359.     if( SUCCEEDED( pd3dDevice->BeginScene() ) )
  360.     { 
  361.         UINT iPass, cPasses;
  362.         V( g_pEffect->Begin(&cPasses, 0) );
  363.         for (iPass = 0; iPass < cPasses; iPass++)
  364.         {
  365.             V( g_pEffect->BeginPass(iPass) );
  366.  
  367.             V( g_Mesh.Render( pd3dDevice ) );
  368.  
  369.             g_pEffect->EndPass();
  370.         }
  371.         g_pEffect->End();
  372.  
  373.         RenderText();
  374.         V( g_HUD.OnRender( fElapsedTime ) );
  375.         
  376.         V( pd3dDevice->EndScene() );
  377.     }
  378. }
  379.  
  380.  
  381. //--------------------------------------------------------------------------------------
  382. // Render the help and statistics text. This function uses the ID3DXFont interface for 
  383. // efficient text rendering.
  384. //--------------------------------------------------------------------------------------
  385. void RenderText()
  386. {
  387.     // The helper object simply helps keep track of text position, and color
  388.     // and then it calls pFont->DrawText( g_pSprite, strMsg, -1, &rc, DT_NOCLIP, g_clr );
  389.     // If NULL is passed in as the sprite object, then it will work however the 
  390.     // pFont->DrawText() will not be batched together.  Batching calls will improves performance.
  391.     CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
  392.  
  393.     // Output statistics
  394.     txtHelper.Begin();
  395.     txtHelper.SetInsertionPos( 5, 5 );
  396.     txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
  397.     txtHelper.DrawTextLine( DXUTGetFrameStats() );
  398.     txtHelper.DrawTextLine( DXUTGetDeviceStats() );
  399.  
  400.     txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
  401.     
  402.     // Draw help
  403.     if( g_bShowHelp )
  404.     {
  405.         const D3DSURFACE_DESC* pd3dsdBackBuffer = DXUTGetBackBufferSurfaceDesc();
  406.         txtHelper.SetInsertionPos( 10, pd3dsdBackBuffer->Height-15*6 );
  407.         txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 0.75f, 0.0f, 1.0f ) );
  408.         txtHelper.DrawTextLine( L"Controls (F1 to hide):" );
  409.  
  410.         txtHelper.SetInsertionPos( 40, pd3dsdBackBuffer->Height-15*5 );
  411.         txtHelper.DrawTextLine( L"Rotate model: Left mouse button\n"
  412.                                 L"Rotate camera: Right mouse button\n"
  413.                                 L"Zoom camera: Mouse wheel scroll\n"
  414.                                 L"Quit: ESC" );
  415.     }
  416.     else
  417.     {
  418.         txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
  419.         txtHelper.DrawTextLine( L"Press F1 for help" );
  420.     }
  421.     txtHelper.End();
  422. }
  423.  
  424.  
  425. //--------------------------------------------------------------------------------------
  426. // Before handling window messages, the sample framework passes incoming windows 
  427. // messages to the application through this callback function. If the application sets 
  428. // *pbNoFurtherProcessing to TRUE, then the sample framework will not process this message.
  429. //--------------------------------------------------------------------------------------
  430. LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing )
  431. {
  432.     // Give the dialogs a chance to handle the message first
  433.     *pbNoFurtherProcessing = g_HUD.MsgProc( hWnd, uMsg, wParam, lParam );
  434.     if( *pbNoFurtherProcessing )
  435.         return 0;
  436.     
  437.     // Pass all remaining windows messages to camera so it can respond to user input
  438.     g_Camera.HandleMessages( hWnd, uMsg, wParam, lParam );
  439.  
  440.     return 0;
  441. }
  442.  
  443.  
  444. //--------------------------------------------------------------------------------------
  445. // As a convenience, the sample framework inspects the incoming windows messages for
  446. // keystroke messages and decodes the message parameters to pass relevant keyboard
  447. // messages to the application.  The framework does not remove the underlying keystroke 
  448. // messages, which are still passed to the application's MsgProc callback.
  449. //--------------------------------------------------------------------------------------
  450. void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown )
  451. {
  452.     if( bKeyDown )
  453.     {
  454.         switch( nChar )
  455.         {
  456.             case VK_F1: g_bShowHelp = !g_bShowHelp; break;
  457.         }
  458.     }
  459. }
  460.  
  461.  
  462. //--------------------------------------------------------------------------------------
  463. // Handles the GUI events
  464. //--------------------------------------------------------------------------------------
  465. void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl )
  466. {
  467.     switch( nControlID )
  468.     {
  469.         case IDC_TOGGLEFULLSCREEN: DXUTToggleFullScreen(); break;
  470.         case IDC_TOGGLEREF:        DXUTToggleREF(); break;
  471.         case IDC_CHANGEDEVICE:     DXUTSetShowSettingsDialog( !DXUTGetShowSettingsDialog() ); break;
  472.     }
  473. }
  474.  
  475.  
  476. //--------------------------------------------------------------------------------------
  477. // This callback function will be called immediately after the Direct3D device has 
  478. // entered a lost state and before IDirect3DDevice9::Reset is called. Resources created
  479. // in the OnResetDevice callback should be released here, which generally includes all 
  480. // D3DPOOL_DEFAULT resources. See the "Lost Devices" section of the documentation for 
  481. // information about lost devices.
  482. //--------------------------------------------------------------------------------------
  483. void CALLBACK OnLostDevice()
  484. {
  485.     if( g_pFont )
  486.         g_pFont->OnLostDevice();
  487.     if( g_pEffect )
  488.         g_pEffect->OnLostDevice();
  489.     g_Mesh.InvalidateDeviceObjects();
  490.  
  491.     SAFE_RELEASE(g_pTextSprite);
  492. }
  493.  
  494.  
  495. //--------------------------------------------------------------------------------------
  496. // This callback function will be called immediately after the Direct3D device has 
  497. // been destroyed, which generally happens as a result of application termination or 
  498. // windowed/full screen toggles. Resources created in the OnCreateDevice callback 
  499. // should be released here, which generally includes all D3DPOOL_MANAGED resources. 
  500. //--------------------------------------------------------------------------------------
  501. void CALLBACK OnDestroyDevice()
  502. {
  503.     SAFE_RELEASE(g_pEffect);
  504.     SAFE_RELEASE(g_pFont);
  505.     g_Mesh.Destroy();
  506. }
  507.  
  508.  
  509.  
  510.